<?php
namespace Home\Controller;
use Think\Controller;
Class GoodsController extends Controller
{
    //1我的发布 2我的产品
    public function home_goods()
    {
        $post_info = I("post.");

        $condition['user_token'] = $post_info['token'];
		
        $condition['user_state'] = 1;

        $get_user_info = D("Index")->get_user_one_info($condition);

		unset($condition);

        if (empty($get_user_info)) {

            $info['code'] = 2;
    
            $info['msg']  = "请登录";      

			echo json_encode($info);die;			
        }
		
		if ($get_user_info['user_open'] != 1) {
			$info['code'] = 3;
			
			$info['msg']  = "您的店铺未开启";
			
			echo json_encode($info);die;
		}		
		
        $condition["goods_status"]  = 0;

        if (intval(I("post.status")) == 1) {

            $condition["goods_status"]  = 1;
        }

        if (intval(I("post.status")) == 2) {

            $condition["goods_status"]  = 2;
        }
        if ($post_info['type'] == 1) {
        	$condition["goods_type"]  = 1;
        }
	
        $condition["goods_state"]   = 1;
		
        $condition["goods_type"]    = $post_info['type'];
        
        $condition["goods_user_id"] = $get_user_info['user_id'];

        $goods = D("Goods")->get_goods_info($condition);

        $info['code'] = 0;
    
        $info['msg']  = "成功";

        $info['data'] = $goods;
        if (!$goods) {
            $info['code'] = 1;
        
            $info['msg']  = "暂无数据";
        }
            
        echo json_encode($info);die;         
    }

    //发布产品
    public function goods()
    {
        $post_info = I("post.");		
		
        $user_token['user_token'] = $post_info['token'];

        $get_user_one_info = D("Index")->get_user_one_info($user_token);

        if (empty($get_user_one_info)) {

            $this->error("请登录!",U("redirect_login"));
        }
        $condition["goods_user_id"] = $get_user_one_info['user_id'];
		
        $res = array();

        $res['goods_user_id'] = $get_user_one_info['user_id'];

        $res['goods_name']    = $post_info['title'];
		
        $res['goods_type']    = $post_info['type'];

        $res['goods_main']	  = $post_info['intor'];
		
        $res['goods_price']	  = $post_info['price'];
		
        $res['goods_fare']	  = $post_info['shipping'];
		
        $res['goods_cc_id']	  = $post_info['cate_id'];
		
        $res['goods_picture_path'] = $post_info['banner']['0'];
		
        $res['goods_created_time'] = date("Y-m-d H:i:s",time());

        $res['goods_updated_time'] = date("Y-m-d H:i:s",time());
		
		if ($post_info['id']) {
			unset($res['goods_created_time']);
			
			$condition['goods_id'] = $post_info['id'];
			
			$result = M("section_goods")->where($condition)->save($res);

		} else {
			unset($condition['goods_id']);
			
			$result = D("Goods")->add_goods_info($res,$condition);
		}
		
		$get_goods_info = M("section_goods")->where("goods_user_id = ".$get_user_one_info['user_id'])->order("goods_id desc")->find();

		$param['category_cc_id'] = $post_info['cate_id'];
		
		$param['category_goods_id'] = $get_goods_info['goods_id'];
		
		$param['category_type']  = $post_info['cate'];
		
		$param['category_created_time'] = date("Y-m-d H:i:s",time());
		
		$param['category_updated_time'] = date("Y-m-d H:i:s",time());
		
		if ($post_info['id']) { 
			unset($param['category_created_time']);
			
			$add_category = M("section_category")->save($param);
		} else {
			$add_category = M("section_category")->add($param);
		}
		
		if ($post_info['id']) {
			M("section_album")->where("album_pid = ".$post_info['id'])->delete();
		}
		if ($post_info['banner']) {
			
			foreach($post_info['banner'] as $v) {
				unset($param);
				$param['album_pid']  = $post_info['id'];
				$param['album_type'] = 1;
				$param['album_path'] = $v;
				M("section_album")->add($param);
			}
		}
		if ($post_info['detail']) {
			
			foreach($post_info['detail'] as $v) {
				unset($param);
				$param['album_pid']  = $post_info['id'];
				$param['album_type'] = 2;
				$param['album_path'] = $v;
				M("section_album")->add($param);
			}
		}
		$info['code'] = 0;
        
        $info['msg']  = "成功";
        
		if (!$result && !$add_category) {
			 $info['code'] = 1;
        
            $info['msg']  = "失败";
        }
        echo json_encode($info);die;
    }

    //修改产品信息
    public function save_goods()
    {

    }

    //删除产品信息
    public function delete_goods()
    {
        $token = trim(I("post.token"));

        $goods_id = intval(I("post.id"));

        $get_user_return = $this->public_token_id($token);

        $condition["goods_user_id"] = $get_user_return['user_id'];

        $condition["goods_id"] = $goods_id;

        $param['goods_state']  = 0;

        $delete_goods = D("Goods")->delete_goods_info($condition,$param);

        if ($delete_goods) {
            $info['code'] = 0;
        
            $info['msg']  = "删除成功";
        } else {
            $info['code'] = 1;
        
            $info['msg']  = "删除失败";
        }
        echo json_encode($info);die;           
    }

    //token过期自动跳转至登陆页面
    public function redirect_login()
    {
        $this->display("Index/login");
    }

    //更改产品状态 上架 下架
    public function up_and_down()
    {
        $token = trim(I("post.token"));

        $goods_id = intval(I("post.id"));

        $get_user_return = $this->public_token_id($token);

        $condition["goods_user_id"] = $get_user_return['user_id'];

        $condition["goods_id"]      = $goods_id;
		
        $condition["goods_state"]   = 1;

        $param['goods_status']      = 0;

        if (intval(I("post.status")) == 0) {
            $param['goods_status'] = 1;
        }

        $delete_goods = D("Goods")->delete_goods_info($condition,$param);

        if ($delete_goods) {
            $info['code'] = 0;
        
            $info['msg']  = "操作成功";
        } else {
            $info['code'] = 1;
        
            $info['msg']  = "操作失败";
        }
        echo json_encode($info);die;           
    }
	
	//产品详情
	public function goods_info()
	{

        $goods_id = intval(I("post.id"));

		$condition['goods_id']      = $goods_id;
				
		$condition['goods_state']   = 1;
		
		$get_goods_one_info = D("Goods")->get_goods_one_info($condition);

		$picture = M("section_album")->where("album_pid = ".$goods_id." and album_state = 1")->select();
		
		$info['code']  = 0;
        
        $info['msg']   = "操作成功";
		
		$info['data']  = $get_goods_one_info;
		
		$info['data_picture']  = $picture;
		if (!$get_goods_one_info) {
			$info['code'] = 1;
        
            $info['msg']  = "操作失败";
		}
		echo json_encode($info);die;     
	}
	
	//产品图片上传
	public function goods_edit_picture()
	{
		$token = trim(I("post.token"));

        $goods_id = intval(I("post.id"));
		
        $goods_type = intval(I("post.type"));

        $get_user_return = $this->public_token_id($token);
		
        $upload = new \Think\Upload();
        $upload->maxSize   =     3145728 ;
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','bmp');
        $upload->rootPath  = "./";
        $upload->savePath  =      './Public/img/goods/';
      
        $info_upload   =   $upload->upload();
		
		if ($info_upload) {
			$picture_info  = $info_upload["picture"]["savepath"].$info_upload["picture"]["savename"];
		
			$upload_images = substr($picture_info,9);
			
			$info['code'] = 0;
		
			$info['msg']  = "成功";
			
			$info['data'] = $upload_images;
			
		} else {
			
			$info['code'] = 1;
		
			$info['msg']  = "失败";
		}
        echo json_encode($info);die;     	
    }
	
	//产品编辑信息
	public function goods_edit()
	{
		$token = trim(I("post.token"));

        $goods_id = intval(I("post.id"));
		
        $get_user_return = $this->public_token_id($token);	
		
	}
	
	//公共产品验证接口
	public function public_token_id($token = '')
	{
		$user_token['user_token'] = $token;

		$get_user_one_info = D("Index")->get_user_one_info($user_token);

		if (empty($get_user_one_info)) {

			$info['code'] = 2;
		
			$info['msg']  = "请登录";
			
			echo json_encode($info);die;     
		}
		return $get_user_one_info;
	}
		
	//我的收藏
	public function goods_college() 
	{
		
		$token = trim(I("post.token"));

        $goods_id = intval(I("post.id"));
		
        $get_user_return = $this->public_token_id($token);
		
		$condition["college_user_id"]  = $get_user_return['user_id'];
		
		$condition["college_goods_id"] = $goods_id;
		
		$condition["college_state"] = 1;
		
		$get_college_one_info = M("section_college")->where($condition)->find();

		if ($get_college_one_info) {
			$info['code'] = 3;
		
			$info['msg']  = "已经收藏";
		
			echo json_encode($info);die;     
		}
		$param = array();
		
		$param['college_user_id']  = $get_user_return['user_id'];

		$param['college_goods_id'] = $goods_id;
		
		$param['college_created_time'] = date("Y-m-d H:i:s",time());
		
		$param['college_updated_time'] = date("Y-m-d H:i:s",time());
		
		$add_college  = D("Goods")->add_college($param);
		
		$info['code'] = 0;
		
		$info['msg']  = "收藏成功";
		if (!$add_college) {
			$info['code'] = 1;
		
			$info['msg']  = "收藏失败";
		}
		echo json_encode($info);die;
	}
	
	
	//收藏列表
	public function college_list()
	{
		$token = trim(I("post.token"));
		
		$get_user_return = $this->public_token_id($token);
				
		$condition['college_user_id'] = $get_user_return['user_id'];
		
		$condition['college_state']   = 1;
		
		$get_college_info = M("section_college")->where($condition)->select();
		
		if ($get_college_info) {
            foreach ($get_college_info as $k => $v) {
                $goods_user_id = $v['college_goods_id'];
				
                $get_college_info[$k]['child'] = D("Goods")->get_goods_info(array('goods_id' => $goods_user_id));
				
            	$info['code'] = 0;
				
				$info['msg']  ="成功";
				
				$info['data'] = $get_college_info;
			}
		} else {
			$info['code'] = 1;
		
			$info['msg']  = "暂无数据";
		}
		echo json_encode($info);die;
	}

	//删除收藏
	public function delete_college()
	{
		$token = trim(I("post.token"));

        $goods_id = intval(I("post.id"));
		
        $get_user_return = $this->public_token_id($token);
		
		$condition['college_user_id']  = $get_user_return['user_id'];
		
		$condition['college_goods_id'] = $goods_id;
		
		$param['college_state'] = 0;

        $delete_college = D("Goods")->delete_college_info($condition,$param);

        if ($delete_college) {
            $info['code'] = 0;
        
            $info['msg']  = "操作成功";
        } else {
            $info['code'] = 1;
        
            $info['msg']  = "操作失败";
        }
        echo json_encode($info);die;           
    }
	
	
	//刷新按钮操作
    public function refresh_goods()
    {

        $goods_id = intval(I("post.id"));

        $token = trim(I("post.token"));

        $user_token['user_token'] = $token;

        $get_user_one_info = D("Index")->get_user_one_info($user_token);

        if (empty($get_user_one_info)) {

            $info['code'] = 2;
        
            $info['msg']  = "请登录";
            
            echo json_encode($info);die;             
        }

        $condition["goods_user_id"]  = $get_user_one_info['user_id'];

        $condition["goods_id"]       = $goods_id;

        $param['goods_updated_time'] = date("Y-m-d H:i:s",time());

        $save_updated_time = D("Goods")->delete_goods_info($condition,$param);

            $info['code'] = 0;
        
            $info['msg']  = "刷新成功";

        if (!$save_updated_time) {
            $info['code'] = 1;
        
            $info['msg']  = "刷新失败";
        }
            echo json_encode($info);die;         
        }
	
}

?>


